package com.bigknow.appstore.web.base.service.impl;

import com.bigknow.appstore.common.inf.server.common.AppInfoHandler;
import com.bigknow.appstore.web.base.entity.FusionConfig;
import com.bigknow.appstore.web.base.service.IFusionService;
import com.bigknow.appstore.web.base.service.ISystemConfigService;
import com.bigknow.appstore.web.console.service.impl.AppInfoService;
import com.bigknow.fusion.common.inf.server.appstore.FusionHandler;
import com.bigknow.fusion.common.inf.server.common.TimeServiceHandler;
import com.bigknow.fusion.common.rpc.client.RPCClient;
import com.bigknow.fusion.web.base.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.*;

/**
 * Created by 李科风 on 2016-09-18.
 */
@Service
public class FusionService implements IFusionService {

    private static final Logger logger = LoggerFactory.getLogger(FusionService.class);

    @Autowired
    private ISystemConfigService sysCfgService;

    private ExecutorService pool = new ThreadPoolExecutor(1, 50,
            20L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());

    @Autowired
    private ScheduledExecutorService scheduledExecutorService;

    private RPCClient client;

    private ScheduledFuture<?> future;

    @PostConstruct
    public void start() {
        future = scheduledExecutorService.scheduleAtFixedRate(() -> {
            try {
                if (client != null && client.isConnected()) {
                    TimeServiceHandler handler = client.getProxy(TimeServiceHandler.class);
                    handler.getTime();
                }
            } catch (Exception e) {
                logger.debug("连接失败", e);
            }
        }, 10, 10, TimeUnit.SECONDS);
    }

    @Override
    public void init(FusionConfig config) {
        if (client == null) {
            client = new RPCClient();
        } else {
            client.close();
            client = new RPCClient();
        }
        String webRoot = System.getProperties().getProperty("web.root");

        client.init(config.getIp(), config.getRpcPort(), webRoot + "/WEB-INF/fusion.jks", pool);
        //client.init(config.getIp(), config.getRpcPort(), webRoot + "/WEB-INF/fusionTest.jks", pool);
        client.connect();
    }

    public FusionHandler getFusionHandler() {
        if (client == null) {
            FusionConfig cfg = sysCfgService.getFusionConfig();
            init(cfg);
        }
        return client.getProxy(FusionHandler.class);
    }

    public <T> void registerRPCInterface(Class<T> interfaceClass, T impl){
        if(client==null){
            FusionConfig cfg = sysCfgService.getFusionConfig();
            init(cfg);
        }
        client.registerRPCInterface(interfaceClass, impl);
    }

    @PreDestroy
    public void destory() {
        client.close();
        client = null;
        future.cancel(false);
    }
}
